package com.sk.bilibili.view.fragment.videoSet;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.sk.bilibili.adapter.vlayout.HorizontalAdapter;
import com.sk.bilibili.adapter.vlayout.videoSet.introduction.HeaderAdapter;
import com.sk.bilibili.adapter.vlayout.videoSet.introduction.OperationAdapter;
import com.sk.bilibili.adapter.vlayout.videoSet.introduction.RecommendAdapter;
import com.sk.bilibili.adapter.vlayout.videoSet.introduction.SelectionAdapter;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.Video;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.FullVideoSetCallBack;
import com.sk.bilibili.event.VideoSetEvent;
import com.sk.bilibili.model.fragment.videoSet.IntroductionModel;
import com.sk.bilibili.view.activity.MemberActivity;
import com.sk.bilibili.view.activity.VideoSetActivity;
import com.sk.bilibili.view.diy.bottomDialog.CollectBottomDialog;
import com.sk.bilibili.view.diy.bottomDialog.LaterBottomDialog;
import com.sk.bilibili.view.diy.bottomDialog.ShareBottomDialog;
import com.sk.bilibili.view.fragment.BaseRefreshFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class IntroductionFragment extends BaseRefreshFragment<IntroductionModel> implements FullVideoSetCallBack {

    private Integer videoSetId;

    public IntroductionFragment(Integer videoSetId) {
        this.videoSetId=videoSetId;
        initData();
    }

    private void initData() {
        collectBottomDialog=new CollectBottomDialog(videoSetId);
        shareBottomDialog=new ShareBottomDialog(0,videoSetId);
        laterBottomDialog=new LaterBottomDialog();
    }

    private HeaderAdapter headerAdapter;
    private OperationAdapter operationAdapter;
    private RecommendAdapter recommendAdapter;
    private HorizontalAdapter<Video> horizontalAdapter;
    private CollectBottomDialog collectBottomDialog;
    private ShareBottomDialog shareBottomDialog;
    private LaterBottomDialog laterBottomDialog;
    private SelectionAdapter selectionAdapter;

    @Override
    protected void initAdapter(DelegateAdapter adapter) {
        headerAdapter = new HeaderAdapter(getContext(),this);
        adapter.addAdapter(headerAdapter);
        operationAdapter = new OperationAdapter(getContext(),this);
        adapter.addAdapter(operationAdapter);
        selectionAdapter = new SelectionAdapter(getContext(), this);
        horizontalAdapter = new HorizontalAdapter<>(getContext(),selectionAdapter);
        adapter.addAdapter(horizontalAdapter);
        recommendAdapter = new RecommendAdapter(getContext(), this);
        adapter.addAdapter(recommendAdapter);
    }

    @Override
    public void onRefresh() {
        model.refreshData(videoSetId);
    }

    public void refreshSuccess(VideoSet videoSet) {
        headerAdapter.setData(videoSet);
        operationAdapter.setData(videoSet);
        if(videoSet.getExtra().getVideos().size()>1){//大于1才更新
            horizontalAdapter.setList(videoSet.getExtra().getVideos());
            selectionAdapter.setSelection(selection);
        }
        cancelRefresh();
    }

    public void addVideoSet(List<VideoSet> records) {
        recommendAdapter.addData(records);
    }

    public void setVideoSet(List<VideoSet> records) {
        recommendAdapter.setData(records);
    }

    public void onSelectionClick(Video video) {
        EventBus.getDefault().post(VideoSetEvent.changeVideo(video));
    }

    public void onFocusClick(Integer id) {
        model.switchFocus(id);
    }

    public void switchFocusSuccess(Focus focus) {
        toast(focus==null?"取消关注成功":"关注成功");
    }

    public void onPraiseClick(Integer id) {
        model.praise(id);
        toast("点赞成功");
    }

    public void onCoinClick(Integer id) {
        model.addCoin(id);
    }

    public void onCollectClick(Integer id) {
        collectBottomDialog.show(getChildFragmentManager());
    }

    public void onForwardClick(Integer id) {
        shareBottomDialog.show(getChildFragmentManager());
    }

    public void addCoinSuccess() {
        toast("投币成功");
    }

    public void addCoinError(String message) {
        toast(message);
    }

    @Override
    public void onClickMember(Integer id) {
        MemberActivity.start(getContext(),id);
    }

    @Override
    public void onClickMore(Integer id) {
        laterBottomDialog.show(getChildFragmentManager(),id);
    }

    @Override
    public void onClickVideoSet(Integer id) {
        VideoSetActivity.start(getContext(),id);
    }

    private int selection;

    public void setSelection(int selection) {
        this.selection=selection;
    }

}